package org.example.blogserver.blog.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.example.blogcommon.base.utils.R;

import org.example.blogpojo.blog.domain.ArticleType;
import org.example.blogpojo.blog.domain.Classity;
import org.example.blogpojo.blog.query.ArticleTypeQuery;
import org.example.blogpojo.blog.vo.ArticleTypeVo;
import org.example.blogserver.blog.mapper.ArticleMapper;
import org.example.blogserver.blog.mapper.ArticleTypeMapper;
import org.example.blogserver.blog.service.ArticleService;
import org.example.blogserver.blog.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;

    @GetMapping
    @Operation (summary = "查所有", description = "查所有")
    public R findAll() {
        List<ArticleType> allArticleType = articleTypeService.getAllArticleType();
        return R.ok(allArticleType);
    }

    @GetMapping("/{id}")
    @Operation(summary = "id查", description = "id查")
    public R findAll(@PathVariable Long id) {
        ArticleType articleTypeById = articleTypeService.getArticleTypeById(id);
        return R.ok(articleTypeById);
    }

    @PostMapping
    @Operation(summary = "分页条件查询", description = "分页条件查询")
    public R queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        PageInfo<ArticleType> articleTypePageInfo = articleTypeService.queryPage(articleTypeQuery);
        System.out.println(articleTypePageInfo);
        return R.ok(articleTypePageInfo);
    }

    @PutMapping
    @Operation(summary = "新增修改", description = "新增修改")
    public R insertandupdate(@RequestBody ArticleType articleType) {
        try {
            Long id = articleType.getId();
            if (id == null) {
                articleTypeService.insertArticleType(articleType);
            }else{
                articleTypeService.updateArticleType(articleType);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "通过id删除", description = "通过id删除")
    public R deleteById(@PathVariable Long id) {
        articleTypeService.deleteArticleType(id);
        return R.ok();
    }

    @PatchMapping
    @Operation(summary = "批量删除", description = "通过id批量删除")
    public R deleteByIds(@RequestBody Long[] ids) {
        articleTypeService.deleteArticleTypeByIds(ids);
        return R.ok();
    }

    @GetMapping("/typeTree/{id}")
    @Operation(summary = "获取无限级数的列表", description = "获取无限级数的列表")
    public R typeTree(@PathVariable Long id) {
        List<ArticleType> allArticleType = articleTypeService.getTypeTree(id);
        return R.ok(allArticleType);
    }

    @GetMapping("/articleTypeStatistics")
    @Operation(summary = "获取文章类型id名称和数量", description = "获取文章类型id名称和数量")
    public List<ArticleTypeVo> getArticleTypeVo() {
        List<ArticleTypeVo> articleTypeVo = articleTypeService.getArticleTypeVo();
        return articleTypeVo;
    }

    @GetMapping("/classityList/{id}")
    public Classity get(@PathVariable Integer id) {
        Map<Integer,Classity> classity = articleTypeService.getClassityList();
        Classity clazz = classity.get(id);
        return clazz;
    }

}
